<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<script src="resources/shared.js"></script>
<input type="file" id="emptyFileInput"></input>
<input type="file" id="emptyFileListInput" multiple></input>
<script>

 // This test cannot be a WPT because it uses the Blink specific APIs to
 // automate file drag/drop.

description("Confirm that IndexedDB can store an empty Blob/File/FileList");

var emptyFileInput = document.getElementById("emptyFileInput");
var emptyFileListInput = document.getElementById("emptyFileListInput");
if (window.eventSender) {
    var fileRect = emptyFileInput.getClientRects()[0];
    var targetX = fileRect.left + fileRect.width / 2;
    var targetY = fileRect.top + fileRect.height / 2;
    eventSender.beginDragWithFiles(['resources/empty.txt']);
    eventSender.mouseMoveTo(targetX, targetY);
    eventSender.mouseUp();
}

function prepareDatabase()
{
    db = event.target.result;
    var trans = event.target.transaction;
    evalAndLog("store = db.createObjectStore('storeName')");
    evalAndLog("store.put('value', 'key')");
    trans.onerror = unexpectedErrorCallback;
    trans.onabort = unexpectedAbortCallback;
}

var blobValidation = ".size == 0";
function testEmptyBlob()
{
    preamble();
    evalAndLog("blob = new Blob([])");
    validateResult("blob", blobValidation, testEmptyDataBlob);
}
function testEmptyDataBlob()
{
    preamble();
    evalAndLog("blob = new Blob(['', '', ''])");
    validateResult("blob", blobValidation, testEmptyNestedBlob);
}
function testEmptyNestedBlob()
{
    preamble();
    evalAndLog("blob = new Blob(['', new Blob([]), ''])");
    validateResult("blob", blobValidation, testEmptyNestedDataBlob);
}
function testEmptyNestedDataBlob()
{
    preamble();
    evalAndLog("blob = new Blob(['', new Blob(['']), ''])");
    validateResult("blob", blobValidation, testEmptyFileInsideBlob);
}
function testEmptyFileInsideBlob()
{
    preamble();
    evalAndLog("file = emptyFileInput.files[0]");
    evalAndLog("blob = new Blob(['', file, ''])");
    validateResult("blob", blobValidation, testEmptyFileInsideNestedBlob);
}
function testEmptyFileInsideNestedBlob()
{
    preamble();
    evalAndLog("file = emptyFileInput.files[0]");
    evalAndLog("blob = new Blob(['', new Blob([file]), ''])");
    validateResult("blob", blobValidation, testEmptyFile);
}

var fileValidation = ".size == 0";
function testEmptyFile()
{
    preamble();
    evalAndLog("file = emptyFileInput.files[0]");
    validateResult("file", fileValidation, testEmptyFileList);
}

var fileListValidation = ".length == 0";
function testEmptyFileList()
{
    preamble();
    evalAndLog("fileList = emptyFileListInput.files");
    validateResult("fileList",
        fileListValidation, finishJSTest);
}

function validateResult(variable, validation, onSuccess)
{
    var keyName = variable + "key";
    debug("");
    debug("validateResult(" + variable + "):");
    shouldBeTrue(variable + validation);
    evalAndLog("transaction = db.transaction('storeName', 'readwrite')");
    evalAndLog("store = transaction.objectStore('storeName')");
    evalAndLog("store.put(" + variable + ", '" + keyName + "')");
    transaction.onerror = unexpectedErrorCallback;
    transaction.onabort = unexpectedAbortCallback;
    var onGetSuccess = function (e) {
      shouldBeTrue("event.target.result" + validation);
      onSuccess();
    }
    transaction.oncomplete = function () {
      doRead(keyName, onGetSuccess);
    }
}

function doRead(keyName, onSuccess)
{
    evalAndLog("transaction = db.transaction('storeName', 'readwrite')");
    evalAndLog("store = transaction.objectStore('storeName')");
    evalAndLog("request = store.get('" + keyName + "')");
    request.onsuccess = onSuccess;
    transaction.onerror = unexpectedErrorCallback;
    transaction.onabort = unexpectedAbortCallback;
}

if (window.eventSender) {
    indexedDBTest(prepareDatabase, testEmptyBlob);
} else {
    alert("Select an empty file using the left input control above to initiate the test");
    document.getElementById("emptyFileInput").onchange = function() { indexedDBTest(prepareDatabase, testEmptyBlob); };
}
</script>
